#!/usr/bin/env python
# -*- coding: utf-8 -*-
#author: Jo�o Caixinha
'''
Created on 27 de Mai de 2012

@author: joaocaixinha
'''

import time
import random
import numpy as np

from KD import KDTree

def medFile(origem, destino):
    ficheiro = open("{0}.dat".format(origem),"r")
    x = []
    y = []
    for line in ficheiro:
        sp = line.split(" ")
        x.append(float(sp[0]))
        sp[1] = sp[1].replace('\n','')
        y.append(float(sp[1]))
    ficheiro.close()
    
    ficheiro = open("{0}.dat".format(destino),"w")
    px = np.array(x)
    py = np.array(y)
    pp = np.polyfit(px,py,3)
    for valX in x:
        valY = np.polyval(pp, valX)
        print str(valX) + '-> ' + str(valY)
        s = str(valX) + ' ' + str(valY) + '\n'
        ficheiro.write(s)
    ficheiro.close()
    print '\n\n'
    
    
'''
testar criacao da arvore atraves de adicao de nós 
'''   
def testAdd(fi, lst):
    k = KDTree([lst.pop(0)], size)
    ficheiro = open("{0}.dat".format(fi),"w")
    for x in range(0, len(lst)):
        print 'add {0} -> {1}'.format(x, lst[x])
        t1 = time.time()
        k.addNode(lst[x])
        t2 = time.time()
        times = t2 - t1
        s = str(x) + ' ' + str(times) + '\n'
        ficheiro.write(s)
    ficheiro.close()
    print '\n\ntest add done ---------------\n\n'
    if type(lst[0]).__name__ == 'tuple':
        medFile("{0}".format(fi), '{0}d_add_med'.format(len(lst[0])))
    else:
        medFile("{0}".format(fi), '1d_add_med')
    return k
        
'''
testar funcao apagar nó um a um
'''
def testDel(fi, lst, k):
    ficheiro = open("{0}.dat".format(fi),"w")
    for x in range(0, len(lst)):
        print 'remove {0} - {1}'.format(x, lst[x])
        t1 = time.time()
        k.removeNode(lst[x])
        t2 = time.time()
        times = t2 - t1
        s = str(x) + ' ' + str(times) + '\n'
        ficheiro.write(s)        
    ficheiro.close()
    print '\n\ntest delete done ---------------\n\n'
    if type(lst[0]).__name__ == 'tuple':
        medFile("{0}".format(fi), '{0}d_delete_med'.format(len(lst[0])))
    else:
        medFile("{0}".format(fi), '1d_delete_med')
        
'''
pesquisa do vizinho mais proximo para 300 pesquisas
'''

def testNearst(fi, lst, k):
    ficheiro = open("{0}.dat".format(fi),"w")
    for x in range(0, len(lst)):
        print 'nearst {0} - {1}'.format(x, lst[x])
        t1 = time.time()
        n = k.findNearst(lst[x])
        t2 = time.time()
        print n
        times = t2 - t1
        s = str(x) + ' ' + str(times) + '\n'
        ficheiro.write(s)
    ficheiro.close()
    print '\n\ntest nearst done ---------------\n\n'
    if type(lst[0]).__name__ == 'tuple':
        medFile("{0}".format(fi), '{0}d_nearst_med'.format(len(lst[0])))
    else:
        medFile("{0}".format(fi), '1d_nearst_med')
        
        
'''----------------------------------------------------------------------------------------------------------------'''

size = 2000

oneD = [(random.randint(0,size)) for x in range(0,size)]
twoD = [(random.randint(0,size), random.randint(0,size)) for x in range(0,size)]
treeD = [(random.randint(0,size), random.randint(0,size),random.randint(0,size)) for x in range(0,size)]


pOneD = [(random.randint(0,size)) for x in range(0,size)]
pTwoD = [(random.randint(0,size), random.randint(0,size)) for x in range(0,size)]
pTreeD = [(random.randint(0,size), random.randint(0,size),random.randint(0,size)) for x in range(0,size)]



oneD_tree = testAdd('oneD_add',oneD)
testNearst('oneD_nearst', pOneD, oneD_tree)
testDel('oneD_del',oneD, oneD_tree)


twoD_tree = testAdd('twoD_add',twoD)
testNearst('twoD_nearst', pTwoD, twoD_tree)
testDel('twoD_del',twoD, twoD_tree)


treeD_tree = testAdd('treeD_add', treeD)
testNearst('treeD_nearst', pTreeD, treeD_tree)
testDel('treeD_del',treeD,  treeD_tree)
















